import * as React from "react"
import {
  message,
} from "antd"
import { IUser } from "../../types"
import User from '../../components/User/index'
import * as api from '../../services/api'

export default function UserList() {
  const [data, setData] = React.useState<IUser[]>([])
  const getData = async () => {
    const handle = message.loading('正在更新数据...', 0)
    const res = await api.allUser()
    if (res.stat === 'OK') {
      setData(res.rows)
    }
    handle()
  }
  const setStatus = async (id: string) => {
    const res = await api.statusUser(id)
    if (res.stat === 'OK') {
      message.success('更改用户状态成功')
    }
  }

  const del = async (id: string) => {
    const res = await api.removeCompanyUser(id)
    if (res.stat === 'OK') {
      message.success('删除成功')
    }
  }

  React.useEffect(() => {
    const handle = message.loading('正在加载数据...', 0)
    const getData = async () => {
      const res = await api.allUser()
      if (res.stat === 'OK') {
        setData(res.rows)
      }
      handle()
    }
    getData()
  }, [])
  return (
    <User
      getData={getData}
      data={data}
      delete={del}
      setStatus={setStatus}
    ></User>)
}
